package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
	"regexp"
	"strconv"
	"strings"
)

type ModelActivity struct{}

func (*ModelActivity) CreateModelActivity(claims tools.BaseClaims, data *admin.ModelActivity) (err error) {

	var account *admin.Account

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	var regionId = utils.ManageCity(account.RegionLink)

	if !utils.ContainsInt(regionId, data.City) {
		return errors.New("你无权设置该城市活动")
	}

	res := global.OC_DB.Where("title = ? AND city = ?", data.Title, data.City).FirstOrCreate(&data)

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该活动已存在")
	}

	return nil
}

func (*ModelActivity) EditModelActivity(claims tools.BaseClaims, data *admin.ModelActivity) (err error) {
	var account *admin.Account

	var region *admin.Region

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&region, data.City).Error

	if err != nil {
		return errors.New("找不到该城市信息")
	}

	res := global.OC_DB.Model(&admin.ModelActivity{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"title":              data.Title,
		"cont":               data.Cont,
		"activity_sub":       data.ActivitySub,
		"cover":              data.Cover,
		"image":              data.Image,
		"init_num":           data.InitNum,
		"status":             data.Status,
		"button_name":        data.ButtonName,
		"back_color":         data.BackColor,
		"text_color":         data.TextColor,
		"border_color":       data.BorderColor,
		"button_color":       data.ButtonColor,
		"activity_sub_color": data.ActivitySubColor,
		"application":        data.Application,
		"city":               data.City,
	})

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该活动已存在")
	}

	return nil
}

func (*ModelActivity) GetModelActivityList(claims tools.BaseClaims, city int) (data []*admin.ModelActivity, err error) {

	var account *admin.Account

	var activities []*admin.ModelActivity

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("找不到该账号信息")
	}

	var strArr = strings.Split(account.RegionLink, "|")
	var ids []int
	for _, v := range strArr {
		var str = strings.Split(v, ",")
		var newStr, _ = strconv.Atoi((str[len(str)-2]))
		ids = append(ids, newStr)
	}

	if city == 0 {
		global.OC_DB.Where("city IN ?", ids).Find(&activities)
		return activities, nil
	} else {
		if !utils.ContainsInt(ids, city) {
			return nil, errors.New("你无法获取该地区活动")
		}
		global.OC_DB.Where("city = ?", city).Find(&activities)

		return activities, nil
	}
}

func (*ModelActivity) GetAllModel(paginate *utils.Page) (page *utils.Page, err error) {

	var list = make([]*admin.ModelActivity, 0)
	global.OC_DB.Scopes(dao.Paginate(dao.Page(*paginate), global.OC_DB)).Find(&list).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)
	paginate.Data = list
	return paginate, nil
}

func (*ModelActivity) GetRegion(claims tools.BaseClaims) (data []*admin.Region, err error) {

	var account *admin.Account

	var regions []*admin.Region

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).Preload("Regions").First(&account).Error

	if err != nil {
		return nil, errors.New("找不到该账号信息")
	}

	if len(account.Regions) != 0 {
		return account.Regions, nil
	} else {
		err = global.OC_DB.Where("manage_id = ?", account.PID).Find(&regions).Error

		if err != nil {
			return nil, errors.New("未知错误500")
		}
		return regions, nil
	}

}

func (*ModelActivity) GetModelActivityListInLouPan(id int, claims tools.BaseClaims) (data []*admin.ModelActivity, err error) {
	var account *admin.Account

	var louPan *admin.LouPan

	var modelActivities []*admin.ModelActivity

	if id == 0 {
		return nil, errors.New("无效的楼盘")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).Preload("Regions").First(&account).Error

	if err != nil {
		return nil, errors.New("找不到该账号信息")
	}

	err = global.OC_DB.First(&louPan, id).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	match, _ := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if !match {
		return nil, errors.New("你无法获取该城市活动模板")
	}

	var strArr = strings.Split(louPan.RegionLink, ",")
	strArr = strArr[1 : len(strArr)-1]
	var ids []int
	for _, v := range strArr {
		id, _ := strconv.Atoi(v)
		ids = append(ids, id)
	}

	global.OC_DB.Where("city IN ?", ids).Find(&modelActivities)

	return modelActivities, nil
}

var ModelActivityService = new(ModelActivity)
